package com.lcxw.hxzbapi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcxw.hxzbapi.dao.mapper.ShortVideoMapper;
import com.lcxw.hxzbapi.entity.pojo.ShortVideo;
import com.lcxw.hxzbapi.entity.pojo.SvLike;
import com.lcxw.hxzbapi.entity.response.SvListResp;
import com.netflix.ribbon.proxy.annotation.ResourceGroup;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component
public class ShortVideoService {

    @Resource
    ShortVideoMapper shortVideoMapper;

    @Resource
    YwpdMemberService ywpdMemberService;

    @Resource
    SvLikeService svLikeService;

    @Resource
    ShortVideoCommentService svCommentService;

    //插入一条短视频
    public int insert(ShortVideo shortVideo) {
        return shortVideoMapper.insert(shortVideo);
    }

//    //通过id删除一条短视频
//    public int deleteById(Integer id) {
//        return shortVideoMapper.deleteById(id);
//    }

    //通过svId删除一条短视频
    public int deleteBySvId(Integer svId) {
        UpdateWrapper<ShortVideo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("svId", svId);
        return shortVideoMapper.delete(updateWrapper);
    }

    //查询一条短视频
    public ShortVideo selectBySvId(Integer svId) {
        return shortVideoMapper.selectById(svId);
    }

    //查询短视频列表
    public List<SvListResp> selectSvList(Long userId, Integer pageNum, Integer pageSize,
                                         boolean isMine) {
        IPage<ShortVideo> shortVideoIPage = new Page<>(pageNum, pageSize);
        QueryWrapper<ShortVideo> queryWrapper = new QueryWrapper<>();
        //用户没有登录修改
        if (isMine && null != userId) {
            queryWrapper.eq("userId", userId);
        }
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("svId");
        shortVideoIPage = shortVideoMapper.selectPage(shortVideoIPage, queryWrapper);
        List<ShortVideo> records = shortVideoIPage.getRecords();
        List<SvListResp> data = new ArrayList<>();
        for (ShortVideo sv : records
                ) {
            SvListResp svListResp = new SvListResp();
            svListResp.setShortVideo(sv);
            svListResp.setImgUrl(ywpdMemberService.selectImgUrlByuserId(sv.getUserId()));
            svListResp.setNickName(ywpdMemberService.selectNickNameByUserId(sv.getUserId()));
            svListResp.setLikes(svLikeService.getLikes(sv.getSvId()));

            //用户没有登录修改
            if (null != userId) {
                int isLike = svLikeService.getSvlike(sv.getSvId(), userId) == null ? 0 : 1;
                svListResp.setIsLike(isLike);
            }
            int commentNum = svCommentService.getCommentNum(sv.getSvId());
            svListResp.setCommentNum(commentNum);

            data.add(svListResp);
        }
        return data;
    }

    //更新一条短视频
    public int update(ShortVideo shortVideo) {
        return shortVideoMapper.updateById(shortVideo);
    }


}
